package 行为型模式.中介者;

import 依赖倒置原则.Custom;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassNameIntermediaryTest
 * @Description 房屋买卖，
 * @Author
 * @Date2022/1/5 17:22
 **/
public class IntermediaryTest {
    public static void main(String[] args) {

        IntermediaryCompany lianjiaAgent = new LianjiaAgent();
        Customer seller = new Seller();
        Customer buyer = new Buyer();

        lianjiaAgent.register(seller);
        lianjiaAgent.register(buyer);
        System.out.println("=========================");
        seller.send();
        System.out.println("=========================");

        buyer.send();


    }
}


abstract class IntermediaryCompany {
    public abstract void register(Customer customer);

    public abstract void reply(Customer customer); //转发
}


class LianjiaAgent extends IntermediaryCompany{
private List<Customer> customers = new ArrayList<>();
    @Override
    public void register(Customer customer) {
        customers.add(customer);
        System.out.println("客户注册进来了");
        customer.setCompany(this);
    }

    @Override
    public void reply(Customer customer) {
        for (Customer ob : customers) {
            if (!ob.equals(customer)) {
                ((Customer) ob).receive();
            }
        }
    }
}

// 抽象客户
abstract class Customer {
    IntermediaryCompany company;

    protected void setCompany(IntermediaryCompany company) {
        this.company = company;
    }

    public abstract void receive();

    public abstract void send();
}

// 具体客户，卖家
class Seller extends Customer {
    @Override
    public void receive() {
        System.out.println("收到买房信息");
    }

    @Override
    public void send() {
        System.out.println("发送卖房信息");
        company.reply(this);
    }
}

// 具体客户买家
class Buyer extends Customer{

    @Override
    public void receive() {
        System.out.println("收到卖房信息");
    }

    @Override
    public void send() {
        System.out.println("发送买房信息");
        company.reply(this);
    }
}
